package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ky.tms.entity.TmsCustomer;
import com.ky.tms.mapper.TmsCustomerMapper;
import com.ky.tms.service.ITmsCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-03-21
 */
@Service
public class TmsCustomerServiceImpl extends ServiceImpl<TmsCustomerMapper, TmsCustomer> implements ITmsCustomerService {

    @Autowired
    private TmsCustomerMapper tmsCustomerMapper;

    @Override
    public IPage<TmsCustomer> page(String userName, TmsCustomer tmsCustomer, Long limit, Long pages) {
        IPage<TmsCustomer> page = new Page(pages,limit);
        QueryWrapper<TmsCustomer> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tmsCustomer.getProjectName())){
            wrapper.like("project_name", tmsCustomer.getProjectName());
        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add(String userName, TmsCustomer tmsCustomer) {
        tmsCustomer.setCreateTime(LocalDateTime.now());
        tmsCustomer.setCreateUser(userName);
        tmsCustomer.setIsValid(1);
        tmsCustomer.setIsLock(0);
        tmsCustomerMapper.insert(tmsCustomer);
    }

    @Override
    public void update(String userName, TmsCustomer tmsCustomer) {

        tmsCustomer.setUpdateTime(LocalDateTime.now());
        tmsCustomer.setUpdateUser(userName);
        tmsCustomerMapper.updateById(tmsCustomer);

    }

    @Override
    public void del(String userName, List<Long> ids) {
        tmsCustomerMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public List<Map<String, Object>> selectList(String userName) {
        return tmsCustomerMapper.selectOption(userName);
    }
}
